package com.tanhua.dubbo.server.controller;

import com.tanhua.dubbo.server.service.TestSoulService;
import com.tanhua.dubbo.server.vo.AnswersVo;
import com.tanhua.dubbo.server.vo.QuestionnaireVo;
import com.tanhua.dubbo.server.vo.TestResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @ClassName: TestSoulController
 * @Author:王晨晗
 * @Description: 灵魂测试
 * @Date: 2022/01/10/14:16
 */
@RestController
@RequestMapping("testSoul")
public class TestSoulController {
    @Autowired
    private TestSoulService testSoulService;

    /**
     * @description 查询问卷列表
     * @return
     */
    @GetMapping
    public ResponseEntity queryQuestionnaireList(){
        //获取所有问卷
        List<QuestionnaireVo> questionnaireVoList = testSoulService.queryQuestionnaireList();
        //返回结果集
        return ResponseEntity.ok(questionnaireVoList);
    }

    /**
     * @description 保存报告
     * @param map
     * @return
     */
    @PostMapping
    public ResponseEntity submitQuestionnaire(@RequestBody Map<String,List<AnswersVo>> map) {
        //获取回答集合
        List<AnswersVo> answers = map.get("answers");
        //保存用户答案,获取到报告id
        String reportId = testSoulService.saveAnswer(answers);
        //返回报告id
        return ResponseEntity.ok(reportId);
    }

    /**
     * @description 根据报告id 获取用户的最新报告
     * @param reportId
     * @return
     */
    @GetMapping("report/{id}")
    public ResponseEntity getUserReport(@PathVariable("id")String reportId){
        //根据报告id获取用户的报告
        TestResultVo testResultVo = testSoulService.getUserReport(Long.valueOf(reportId));
        //响应用户报告
        return ResponseEntity.ok(testResultVo);
    }
}
